from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from blog.models import *
from blog.forms import *
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.core.mail import send_mail
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
import random, string
# Create your views here.


def GenNumber(length): #生成随机数
    chars=string.digits
    return ''.join([random.choice(chars) for i in range(length)])



def answer(request):
    context = {}
    return render(request, "answer.html", context)


def detail(request,id):
    context = {}
    return render(request, "detail.html", context)


def home(request):

    context = {}
    return render(request, "home.html", context)


 

 




def profile(request,id):
    context = {}
    user = UserProfile.objects.get(id=id)
    ask_question = Question.objects.filter(author=user)
    answer = Answer.objects.filter(author=user)
    context['user'] = user
    context['questions'] = ask_question
    context['answers'] = answer
    
    return render(request, "profile.html", context)




def register(request):
    if request.user.is_authenticated():
        return redirect(to='home')
    context = {}
    if request.method == "GET":
        return render(request, 'register.html')
    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = User.objects.create_user(
                username=email, email=email, password=password)
            user.save()
            userprofile = UserProfile(belong_to=user, name=name, avatar="avatars/u=479936485,4040074304&fm=21&gp=0.jpg")
            userprofile.save()
            return redirect(to='Login')
        context['form'] = form
        return render(request, 'register.html', context)


def Login(request):
    if request.user.is_authenticated():
        return redirect(to='home')
    if request.method == "GET":
        return render(request, 'login.html')
    if request.method == "POST":
        form = LoginForm(request.POST)

        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = authenticate(username=email, password=password)
            if user:
                login(request, user)
                return redirect(to='home')
            
            else:
                context = {}
                context['error'] = '你输入的账户或密码错误'
                return render(request, 'login.html', context)

        context = {}
        context['form'] = form
        return render(request, 'login.html', context)
        

def search(request):
    context = {}
    return render(request, "search.html", context)


def test_view(request):
    return render(request, "test.html")





def forget_password(request):
    return render(request, 'password.html')
